package com.xinnet.mms.allot.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import com.xinnet.core.dao.impl.BaseDaoImpl;
import com.xinnet.mms.allot.dao.CustomAllotDao;
import com.xinnet.mms.entity.Area;
import com.xinnet.mms.entity.CustomAllotConfig;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.CustomerCare;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.entity.PushHistory;
import com.xinnet.mms.entity.ScanningList;
@Repository("customerAllotDao")
public class CustomAllotDaoImpl<E> extends BaseDaoImpl<Customer, Long> implements CustomAllotDao{
	
	//根据客户地域标签和产品标签 得到对应的部门
	@Override
	public List<Dept> getDept(Customer customer) throws SQLException {
		if(customer.getProductLabel()==null || "".equals(customer.getProductLabel())){
			customer.setProductLabel("NYZJ");
		}
		return this.getSqlSession().selectList("customerallot.getDeptByLable", customer);
	}
	
	//用取得的部门编号获取该部门将要被分配的员工
	@Override
	public Emp getEmp(@Param(value="deptno") String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getEmpsByDeptno", deptno);
	}
	
	//用取得的部门标识 得到负责的产品信息
	@Override
	public Product getProductByDeptno(@Param(value="deptno") String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getProductByDeptno", deptno);
	}

	//查询手机号和email匹配 两项一致返回客户信息
	@Override
	public Customer checkReported(Customer Customer) throws SQLException {
		Customer reported=this.getSqlSession().selectOne("customerallot.getCheckReported",Customer);
		if(reported!=null)
			return reported;
		return null;
	}
	
	//通过人员标识查询人员信息
	@Override
	public Emp getEmpByEmpno(@Param(value="empno") String empno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getEmpByEmpno", empno);
	}
	
	//通过人员所属部门标识查询部门信息
	@Override
	public Dept getDeptByDeptno(@Param(value="id") String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getDeptByDeptno", deptno);
	}
	
	//重置部门所有员工排座标记
	@Override
	public void restAllAllotLabel(@Param(value="deptno") String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.restAllAllotLabel",deptno);
	}
	
	//将该员工排座标记设为不可排座
	@Override
	public void restAllotLabel(@Param(value="empno") String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllotLabel",empno);
	}
	
	//增加流转数据
	@Override
	public void addHistory(PushHistory pushHistory) throws SQLException {
		this.getSqlSession().insert("customerallot.addHistory",pushHistory);
	}
	
	//获得报备客户信息匹配的客户数量
	@Override
	public Integer getCheckReportedCount(Customer customer) throws SQLException {
		return (Integer)this.getSqlSession().selectOne("customerallot.getCheckReportedCount",customer);
	}
	
	//将客户信息插入客户表
	@Override
	public void addNewCustomer(Customer customer) throws SQLException {
		this.getSqlSession().insert("customerallot.addNewCustomer",customer);
	}
	
	//修改客戶類型為意向客戶
	@Override
	public void updateCustomerType(@Param(value="cusId") String cusId) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerType",cusId);
	}
	
	@Override
	public void updateCustomerIsAllot(Customer customer) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerIsAllot",customer);
	}
	
	//修改客戶類型
	@Override
	public void updateCustomerTypeByCusId(Customer customer) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerTypeByCusId",customer);
	}
	
	public void updateCustomerAreaByCusId(Customer customer) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerAreaByCusId",customer);
	}

	//修改客户类型为服务期客户
	@Override
	public void updateCustomerTypetoService(String cusId) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerTypeToService", cusId);
	}

	//将客户服务信息放入客户服务表
	@Override
	public void addCustomerService(CustomerCare customerCare) throws SQLException {
		this.getSqlSession().insert("customerallot.addCustomerService", customerCare);
	}

	//通过客户ID获取客户信息
	@Override
	public Customer getCustomerById(String cusId) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getCustomerById", cusId);
	}

	//通过用户ID查询客户被分配状态
	@Override
	public Integer getCustomerState(String cusId) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getCustomerState", cusId);
	}

	/**根据客户ID 推送人ID 客户类型 查询最近的一条推送历史*/
//	@Override
//	public PushHistory getPushHistoryByCustomerInfo(Customer customer) throws SQLException {
//		return this.getSqlSession().selectOne("customerallot.getPushHistoryByCustomerInfo",customer);
//	}

	/**根据流转历史ID 将流转历史接收状态改为拒绝*/
	@Override
	public void updatePushHistoryForRefuse(String id) throws SQLException {
		this.getSqlSession().update("customerallot.updatePushHistoryForRefuse",id);
	}

	/**用部门编号查询该部门负责的区域*/
	@Override
	public Area getAreaByDeptno(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getAreaByDeptno",deptno);
	}

	/**获取部门的分配规则*/
	@Override
	public CustomAllotConfig getAllotConfigByDeptno(CustomAllotConfig customAllotConfig) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getAllotConfigByDeptno",customAllotConfig);
	}

	/**用产品标签查出产品*/
	@Override
	public Product getProductByProductId(@Param(value="productId") String productId) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getProductByProductId",productId);
	}

	/**获取默认分配的产品线*/
	@Override
	public Product getDefaultProduct() throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getDefaultProduct");
	}

	/**根据ID删除客户*/
	@Override
	public void delCustomer(@Param(value="cusId") String cusId) throws SQLException {
		this.getSqlSession().delete("customerallot.delCustomer",cusId);
	}

	/**取得父节点部门*/
	@Override
	public Dept getParentDept(@Param(value="deptno") String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getParentDept",deptno);
	}

	/**修改人员客户关系*/
	@Override
	public void updateEmpAndCustomer(Customer customer) throws SQLException {
		this.getSqlSession().update("custommix.updateEmpAndCustomer",customer);
	}

	/**用客户ID和销售ID找出超时的推送记录并删除*/
	@Override
	public void delPushHistoryByCusIdAndEmpno(Customer customer) throws SQLException {
		this.getSqlSession().delete("customerallot.delPushHistoryByCusIdAndEmpno",customer);
	}

	/**修改客户产品标签*/
	@Override
	public void updateCustomerLabel(String cusId,String productId) throws SQLException {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setProductId(productId);
		this.getSqlSession().update("customerallot.updateCustomerLabel",customer);
	}

	/**通过productCode获取product*/
	@Override
	public Product getProductByProductCode(String productCode) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getProductByProductCode",productCode);
	}

	/**
	 * 查询支持人工推送的emp
	 */
	@Override
	public Emp getEmpsByDeptnoIsArtifical(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getEmpsByDeptnoIsArtifical",deptno);
	}

	/**
	 * 根据手机邮箱获取400乐语客户
	 */
	@Override
	public Customer get400Customer(Customer customer) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.get400Customer",customer);
	}

	/**
	 * 修改客户信息
	 */
	@Override
	public void updateCustomerInfo(Customer customer) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerInfo",customer);
	}
	
	/**
	 * 修改客户信息
	 */
	@Override
	public void updateCustomerSource(Customer customer) throws SQLException {
		this.getSqlSession().update("customerallot.updateCustomerSource",customer);
	}

	/**
	 * 根据cusId和productLabel查询客户表记录数
	 */
	@Override
	public Integer getServiceCustomerCountByCusIdAndProductLabel( Customer customer) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getServiceCustomerCountByCusIdAndProductLabel",customer);
	}

	/**
	 * 获取相同手机号/邮箱的客户
	 */
	@Override
	public List<Customer> getCheckAlikeCustomer(Customer customer) throws SQLException {
		return this.getSqlSession().selectList("customerallot.getCheckAlikeCustomer",customer);
	}
	
	/**
	 * 获取相同手机号/邮箱的客户
	 */
	@Override
	public List<Customer> getCheckCustomerByPhone(Customer customer) throws SQLException {
		return this.getSqlSession().selectList("customerallot.getCheckCustomerByPhone",customer);
	}
	
	

	/**
	 * 添加超时扫描记录
	 */
	@Override
	public void addScanningList(ScanningList scanningList) throws Exception {
		this.getSqlSession().insert("customerallot.addScanningList",scanningList);
	}

	/**
	 * 删除超时扫描记录
	 */
	@Override
	public void delScanningList(String id) throws SQLException {
		this.getSqlSession().delete("customerallot.delScanningList",id);
	}

	/**
	 * 获取所有超时扫描记录
	 */
	@Override
	public List<ScanningList> getAllScanningList() throws SQLException {
		List<ScanningList> list=this.getSqlSession().selectList("customerallot.getAllScanningList");
		if(list == null || list.isEmpty()) {
			return null;
		}
		return list;
	}

	@Override
	public int countByEmpno(String empno) throws SQLException {
		
		return this.getSqlSession().selectOne("customerallot.countByEmpno");
	}

	//修改扫描表记录
	@Override
	public void updateScanningListById(ScanningList scann) throws SQLException {
		this.getSqlSession().update("customerallot.updateScanningListById",scann);
	}

	/**
	 * 重置该部门超时重新分配标签
	 */
	@Override
	public void resetScannAllAllotLabel(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannAllAllotLabel",deptno);
	}

	/**
	 * 获取可以被超时分配的员工
	 */
	@Override
	public Emp getScannEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getScannEmp",deptno);
	}

	/**
	 * 将员工超时未接收标签设为不可接收
	 */
	@Override
	public void resetScannAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannAllotLabel",empno);
	}
	
	/**
	 * 
	 *
	 * 功能描述：获取要清洗的数据
	 * 
	 * @author 湛智
	 *
	 * @throws 2014年8月13日
	 *
	 */
	public List<Customer> getFlushDataCustomer() throws SQLException {
		return this.getSqlSession().selectList("customerallot.getFlushDataCustomer");
	}

	@Override
	public List<Customer> getCustomerByAllotState(Customer customer)
			throws SQLException {
		return this.getSqlSession().selectList("customerallot.getCustomerByAllotState",customer);
	}

	/**
	 * 获取可分配服务期客户的员工
	 */
	@Override
	public Emp getServiceEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getServiceEmp",deptno);
	}

	/**
	 * 获取可分配意向客户的员工
	 */
	@Override
	public Emp getIntetionEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getIntetionEmp",deptno);
	}

	/**
	 * 获取可分配新注册客户的员工
	 */
	@Override
	public Emp getNewEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getNewEmp",deptno);
	}

	/**
	 * 标记服务期分配的销售标记
	 */
	@Override
	public void resetServiceAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetServiceAllotLabel",empno);
	}

	/**
	 * 重置服务期分配的所有销售标记
	 */
	@Override
	public void resetAllServiceAllotLabel(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllServiceAllotLabel",deptno);
	}

	/**
	 * 标记新注册分配的销售标记
	 */
	@Override
	public void resetNewAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetNewAllotLabel",empno);
	}

	/**
	 * 重置新注册分配的所有销售标记
	 */
	@Override
	public void resetAllNewAllotLabel(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllNewAllotLabel",deptno);
	}

	/**
	 * 标记意向分配的销售标记
	 */
	@Override
	public void resetIntetionAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetIntetionAllotLabel",empno);
	}

	/**
	 * 重置意向分配的所有销售标记
	 */
	@Override
	public void resetAllIntetionAllotLabel(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllIntetionAllotLabel",deptno);
	}

	/**
	 * 获取可被超时重新分配新注册会员的销售
	 */
	@Override
	public Emp getScannNewEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getScannNewEmp",deptno);
	}

	/**
	 * 获取可被超时重新分配意向会员的销售
	 */
	@Override
	public Emp getScannIntetionEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getScannIntetionEmp",deptno);
	}

	/**
	 * 获取可被超时重新分配新服务期会员的销售
	 */
	@Override
	public Emp getScannServiceEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getScannServiceEmp",deptno);
	}

	/**
	 * 标记超时重新分配的新注册客户销售
	 */
	@Override
	public void resetScannNewEmp(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannNewEmp",empno);
	}

	/**
	 * 标记超时重新分配的服务期客户销售
	 */
	@Override
	public void resetScannServiceEmp(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannServiceEmp",empno);
	}

	/**
	 * 标记超时重新分配的意向客户销售
	 */
	@Override
	public void resetScannIntetionEmp(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannIntetionEmp",empno);
	}

	/**
	 * 重置超时重新分配的新注册客户所有销售标记
	 */
	@Override
	public void resetAllScannNewEmp(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllScannNewEmp",deptno);
	}

	/**
	 * 重置超时重新分配的服务期客户所有销售标记
	 */
	@Override
	public void resetAllScannServiceEmp(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllScannServiceEmp",deptno);
	}

	/**
	 * 重置超时重新分配的意向客户所有销售标记
	 */
	@Override
	public void resetAllScannIntetionEmp(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllScannIntetionEmp",deptno);
	}

	/**
	 * 获取可被分配未注册会员的销售
	 */
	@Override
	public Emp getNotRegEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getNotRegEmp",deptno);
	}

	/**
	 * 标记超时重新分配的未注册客户销售
	 */
	@Override
	public void resetNotRegAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetNotRegAllotLabel",empno);
	}

	/**
	 * 重置分配的未注册客户所有销售标记
	 */
	@Override
	public void resetAllNotRegAllotLabel(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllNotRegAllotLabel",deptno);
	}

	/**
	 * 获取可被超时重新分配未注册会员的销售
	 */
	@Override
	public Emp getScannNotRegEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getScannNotRegEmp",deptno);
	}

	/**
	 * 标记超时重新分配的未注册客户所有销售
	 */
	@Override
	public void resetScannNotRegEmp(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannNotRegEmp",empno);
	}

	/**
	 * 重置超时重新分配的未注册客户所有销售所有标记
	 */
	@Override
	public void resetAllScannNotRegEmp(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllScannNotRegEmp",deptno);
	}

	/**
	 * 获取可被分配二次开发会员的销售
	 */
	@Override
	public Emp getTwiceEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getTwiceEmp",deptno);
	}

	/**
	 * 标记分配的二次开发客户销售
	 */
	@Override
	public void resetTwiceAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetTwiceAllotLabel",empno);
	}

	/**
	 * 重置分配的二次开发客户所有销售所有标记
	 */
	@Override
	public void resetAllTwiceAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllTwiceAllotLabel",empno);
	}

	/**
	 * 获取可被超时重新分配二次开发会员的销售
	 */
	@Override
	public Emp getScannTwiceEmp(String deptno) throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getScannTwiceEmp",deptno);
	}

	/**
	 * 标记超时重新分配的二次开发客户销售
	 */
	@Override
	public void resetScannTwiceEmp(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetScannTwiceEmp",empno);
	}

	/**
	 * 重置超时重新分配的二次开发客户所有销售所有标记
	 */
	@Override
	public void resetAllScannTwiceEmp(String deptno) throws SQLException {
		this.getSqlSession().update("customerallot.resetAllScannTwiceEmp",deptno);
	}

	/**
	 * 标记电销销售分配标签
	 */
	@Override
	public void resetSpecialEmpAllotLabel(String empno) throws SQLException {
		this.getSqlSession().update("customerallot.resetSpecialEmpAllotLabel",empno);
	}

	@Override
	public CustomAllotConfig getPollInterval() throws SQLException {
		return this.getSqlSession().selectOne("customerallot.getPollInterval");
	}

	@Override
	public void updatePollInterval(Integer pollInterval) throws SQLException {
		this.getSqlSession().update("customerallot.updatePollInterval",pollInterval);
	}
}
